package org.moore.controller;

import org.moore.model.User;
import org.moore.utils.Data;
import org.moore.utils.UserUtils;
import org.moore.view.GameFrame;
import org.moore.view.WelcomeJFrame;
import org.moore.view.WelcomePanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Objects;
// 确认登录监听类
public class YesLoginButtonListener implements MouseListener {
    private final WelcomePanel welcomePanel;
    private final GameFrame gameFrame;
    private final WelcomeJFrame welcomeJFrame;

    public YesLoginButtonListener(WelcomePanel welcomePanel, GameFrame gameFrame, WelcomeJFrame welcomeJFrame) {
        this.welcomePanel = welcomePanel;
        this.gameFrame = gameFrame;
        this.welcomeJFrame = welcomeJFrame;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // 创建一个新的字体对象
        Font newFont = new Font("宋体", Font.PLAIN, 18);
        // 设置所有的OptionPane字体
        UIManager.put("OptionPane.messageFont", newFont);
        UIManager.put("OptionPane.buttonFont", newFont);
        // 获取文本内容
        String account = WelcomePanel.getLoginJLabel().getAccText().getText().trim();
        String password = new String(WelcomePanel.getLoginJLabel().getPwdText().getPassword()).trim();
        String checkNum = WelcomePanel.getLoginJLabel().getCheckNum().getText().trim();
        String num = WelcomePanel.getLoginJLabel().getCheckText().getText().trim();
        System.out.println(num);
        // 检测验证码是否为空
        if (num.isEmpty()) {
            JOptionPane.showMessageDialog(welcomePanel, "验证码不得为空！");
            return;
        }
        // 检测密码是否为空
        if (account.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(welcomePanel, "账号或密码不得为空！");
            return;
        }
        // 检测验证码是否一致
        if (!checkNum.equals(num)) {
            JOptionPane.showMessageDialog(welcomePanel, "验证码输入错误！");
            return;
        }
        //检测账号密码是否正确
        if (UserUtils.findAccAndPwdByAdmin(account, password) == null) {
            //登录失败
            JOptionPane.showMessageDialog(welcomePanel,"账号或密码有误！");

        }else {
            JOptionPane.showMessageDialog(welcomePanel, "登录成功！！欢迎您【" + Objects.requireNonNull(UserUtils.findAccAndPwdByAdmin(account, password)).getName() + "】");
            Objects.requireNonNull(UserUtils.findAccAndPwdByAdmin(account, password));
            gameFrame.setTitle("武侠消消乐【玩家[" + Objects.requireNonNull(UserUtils.findAccAndPwdByAdmin(account, password)).getName() + "]UID[" + User.getId() + "]】");
            gameFrame.setVisible(true);
            welcomeJFrame.setVisible(false);
            Data.TOURIST = false;
            GameFrame.getGameBar().getRoadItem().setEnabled(true);
            GameFrame.getGameBar().getRankingListItem().setEnabled(true);
        }

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
